package com.nbufe.controller;

import com.nbufe.domain.ResponseResult;
import com.nbufe.domain.dto.target.AddTargetDto;
import com.nbufe.domain.dto.target.EditTargetDto;
import com.nbufe.domain.dto.target.QueryEmissionTargetDto;
import com.nbufe.domain.dto.target.QueryTargetPageDto;
import com.nbufe.domain.dto.trade.CarbonTradeDto;
import com.nbufe.domain.dto.trade.QueryTradePage;
import com.nbufe.domain.entity.CarbonStatistics;
import com.nbufe.service.CarbonStatisticsService;
import com.nbufe.service.CarbonTargetService;
import com.nbufe.service.CarbonTradingService;
import com.nbufe.service.DataService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 碳交易接口Api
 *
 * @Author luzhiheng
 * @Date 2023/12/24 17:42
 */
@RestController
@RequestMapping("/carbon_trade")
public class CarbonTradeController {

    @Resource
    private DataService dataService;

    @Resource
    private CarbonTradingService carbonTradingService;

    @Resource
    private CarbonStatisticsService carbonStatisticsService;

    @PostMapping("/marketInfo/{type}")
    public ResponseResult queryMarketInfo(@PathVariable Integer type) {
        return carbonTradingService.queryMarketInfo(type);
    }

    @PostMapping("/queryCarbonAssertInfo")
    public ResponseResult queryMarketInfo() {
        return carbonTradingService.queryCarbonAssertInfo();
    }

    @PostMapping("/queryCurrentCarbonAssert")
    public ResponseResult queryCurrentCarbonAssert() {
        return carbonTradingService.queryCurrentCarbonAssert();
    }

    @PostMapping("/queryCarbonTradeCompare")
    public ResponseResult queryCarbonTradeCompare() {
        return carbonTradingService.queryCarbonTradeCompare();
    }

    @ApiOperation("碳资产与实际排放对比")
    @PostMapping("/queryCarbonAssertCompare")
    public ResponseResult queryCarbonAssertCompare() {
        return carbonTradingService.queryCarbonAssertCompare();
    }

    @ApiOperation("分页查询碳交易列表")
    @PostMapping("/queryTradeList")
    public ResponseResult queryTradeList(@RequestBody QueryTradePage queryTradePage) {
        return carbonTradingService.queryTradeList(queryTradePage);
    }

    @ApiOperation("查询碳资产相关信息")
    @PostMapping("/queryCarbonAssert")
    public ResponseResult queryCarbonAssert() {
        return carbonTradingService.queryCarbonAssert();
    }

    @ApiOperation("碳资产交易")
    @PostMapping("/carbonAssertTrade")
    public ResponseResult carbonAssertTrade(@RequestBody Map<String, Object> map) {
        return carbonTradingService.carbonAssertTrade(map);
    }

    @ApiOperation("查询碳资产与实际排放对比")
    @PostMapping("/carbonEmissionCompare")
    public ResponseResult carbonEmissionCompare() {
        return carbonTradingService.carbonEmissionCompare();
    }

    @PostMapping("/queryNewPrice")
    public ResponseResult queryNewPrice() {
        return carbonTradingService.queryNewPrice();
    }

    @PostMapping("/buyCarbon")
    public ResponseResult buyCarbon(@RequestBody CarbonTradeDto dto) {
        return carbonTradingService.buyCarbon(dto);
    }

    @PostMapping("/sellCarbon")
    public ResponseResult sellCarbon(@RequestBody CarbonTradeDto dto) {
        return carbonTradingService.sellCarbon(dto);
    }

    @PostMapping("/updateTrade")
    public ResponseResult updateTrade() {

        Double totalCO2 = dataService.queryTotalCO2();
        List<CarbonStatistics> carbonStatisticsList = carbonStatisticsService.list();
        CarbonStatistics statistics = carbonStatisticsList.get(0);
        statistics.setCarbonEmission(totalCO2);
        Double totalCcer = carbonTradingService.queryTotalCCER();
        Double totalQuota = carbonTradingService.queryTotalQuota();
        statistics.setTotalCcer(totalCcer);
        statistics.setTotalQuota(totalQuota);

        carbonStatisticsService.updateById(statistics);
        return ResponseResult.okResult();
    }
}